<?php
namespace app\admin\controller;

use app\admin\logic\GcategoryLogic;
use app\admin\logic\GoodsLogic;
use app\BaseController;
use think\facade\Db;
use think\facade\Request;
use think\facade\View;
//分类页
class Gcategory extends BaseController {
    //cate分类列表
    public function cate(){
        //where条件拼接
        $where=GcategoryLogic::cateWhere();
        //sql语句
        $cate=\app\admin\model\Gcategory::cateList($where);
        //没有下一级是不可点
        $cate=GcategoryLogic::cateData($cate);
        View::assign('cate',$cate);
        //添加分类
        $parent=GoodsLogic::goodCate();
        View::assign('parent',$parent);
        return View::fetch();
    }
    //分类添加
    public function cateAdd(){
        if (Request::isPost()){
            $post=Request::post();
            $res =\app\admin\model\Gcategory::cateAddInfo($post);
            if ($res) {
                $this->success('添加成功', url('cate'));
            } else {
                $this->error('添加失败');
            }
        }else{
            return View::fetch();
        }


    }
    //cate 分类修改
    public function cateEdit(){
        if (Request::isPost()){
            $param=Request::param();
            $res=\app\admin\model\Gcategory::cateEditInfo($param);
            $get=Request::get();
            if ($res) {
                unset($get['cate_id']);
                $this->success('修改成功', url('cate',$get));
            } else {
                $this->error('修改失败');
            }
        }else{
            $param=Request::param();
            $find=\app\admin\model\Gcategory::cateEditInfo($param);
            View::assign(  $find->toArray());
            return View::fetch();
        }




    }

    //cate 分类删除
     public function cateDel(){
     $get=Request::get();
     list($msg,$res)=\app\admin\model\Gcategory::cateDelInfo($get);
         if ($res){
             $this->success($msg,url('cate'));
         }elseif(!$res){
             $this->error($msg);
         }else{
             $this->error($msg);
         }
      }

}